from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
import getpass

class Command(BaseCommand):
    help = "gives the user a superuser"

    def handle(self, *args, **options):
        if len(args) > 1:
            raise CommandError("need exactly one or zero arguments for username")
        if args:
            username, = args
        else:
            username = getpass.getuser()
        try:
            u = User.objects.get(username=username)
        except User.DoesNotExist:
            raise CommandError("user '%s' does not exist" % username)

        if not u.is_superuser:
            u.is_superuser = True
            u.save()
        else:
            raise CommandError("user '%s' is already superuser" % username)